package 字节;

import java.util.ArrayList;
import java.util.List;

public class 螺旋矩阵 {
    List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res=new ArrayList<>();
        int up=0,down= matrix.length-1;
        int left=0,right=matrix[0].length-1;
        while (res.size()<matrix.length*matrix[0].length){
            if(up<=down){
                for (int i=left;i<=right;i++){
                    res.add(matrix[up][i]);
                }
                up++;
            }

            if(left<=right){
                for (int i=up;i<=down;i++){
                    res.add(matrix[i][right]);
                }
                right--;
            }
            if(up<=down){
                for (int i=right;i>=left;i--){
                    res.add(matrix[down][i]);
                }
                down--;
            }

            if(left<=right){
                for (int i=down;i>=up;i--){
                    res.add(matrix[i][left]);
                }
                left++;
            }

        }
        return res;

    }
}
